/*
 * Copyright 2010 Electronic Business Systems Ltd.
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * http://www.apache.org/licenses/LICENSE-2.0
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package gr.ebs.gssdroid.resource;

import java.io.Serializable;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;




/**
 * @author kman
 *
 */
public abstract class RestResource implements Serializable{
	String uri;

	public RestResource(String aUri) {
		super();
		setUri(aUri);
	}

	/**
	 * Retrieve the uri.
	 *
	 * @return the uri
	 */
	public String getUri() {
		return uri;
	}

	/**
	 * Modify the uri.
	 *
	 * @param aUri the path to set
	 */
	public void setUri(String aUri) {
		uri = aUri;
		if (uri!=null) {
			// Remove any parameter part
			int qm = uri.indexOf('?');
			if (qm>=0) uri = uri.substring(0, qm);
		}
	}

	public abstract void createFromJSON(String text) throws JSONException;

	protected String unmarshallString(JSONObject obj, String key){
		try {
			return obj.getString(key);
		} catch (JSONException e) {
			return null;
		}
	}

	protected Boolean unmarshallBoolean(JSONObject obj, String key){
		try {
			return obj.getBoolean(key);
		} catch (JSONException e) {
			return null;
		}
	}
	
	protected JSONArray getJSONArray(JSONObject obj, String key){
		try {
			return obj.getJSONArray(key);
		} catch (JSONException e) {
			return null;
		}
	}
	
	protected JSONObject getJSONObject(JSONObject obj, String key){
		try {
			return obj.getJSONObject(key);
		} catch (JSONException e) {
			return null;
		}
	}

	public Long getLastModifiedDate(){
		return null;
	}

	public static native String getDate(Long ms)/*-{
		return (new Date(ms)).toUTCString();
	}-*/;

	public abstract String getLastModifiedSince();
}
